const path = require('path')
const webpack = require('webpack')
const createThemeColorReplacerPlugin = require('./config/plugin.config')
const NODE_ENV = process.env.NODE_ENV
const VUE_APP_PREVIEW = process.env.VUE_APP_PREVIEW
const VUE_APP_EMAIL = process.env.VUE_APP_EMAIL
const VUE_APP_AUTHOR = process.env.VUE_APP_AUTHOR
console.log(`环境变量信息:${JSON.stringify({ NODE_ENV, VUE_APP_PREVIEW, VUE_APP_EMAIL, VUE_APP_AUTHOR })}`)

function resolve(dir) {
  return path.join(__dirname, dir)
}

const isProd = process.env.NODE_ENV === 'production'

const assetsCDN = {
  // webpack build externals
  externals: {
    vue: 'Vue',
    'vue-router': 'VueRouter',
    vuex: 'Vuex',
    axios: 'axios',
    'animate.css':'animate.css',
    'editor.md':'editor.md',
    aos:'aos'
  },
  css: [
    'https://cdn.jsdelivr.net/npm/ant-design-vue@1.5.0/dist/antd.min.css',
    'https://cdn.bootcdn.net/ajax/libs/animate.css/3.7.2/animate.min.css',
    'https://cdn.bootcdn.net/ajax/libs/aos/2.3.4/aos.css',
  ],
  js: [
    'https://cdn.jsdelivr.net/npm/ant-design-vue@1.5.0/dist/antd.min.js',
    'https://cdn.bootcdn.net/ajax/libs/vue/2.6.10/vue.min.js',
    'https://cdn.bootcdn.net/ajax/libs/vue-router/3.1.3/vue-router.min.js',
    'https://cdn.bootcdn.net/ajax/libs/vuex/3.1.1/vuex.min.js',
    'https://cdn.bootcdn.net/ajax/libs/axios/0.19.0/axios.min.js',
    'https://cdn.bootcdn.net/ajax/libs/editor-md/1.5.0/editormd.min.js',
    'https://cdn.bootcdn.net/ajax/libs/aos/2.3.4/aos.js',
  ]
}

// vue.config.js
const vueConfig = {
  publicPath: process.env.NODE_ENV === 'development' ? '/' : '/',
  configureWebpack: {
    // webpack plugins
    plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'windows.jQuery': 'jquery'
      }),
      // Ignore all locale files of moment.js
      new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)
    ],
    // if prod, add externals
    externals: isProd ? assetsCDN.externals : {}
  },

  chainWebpack: (config) => {
    config.resolve.alias
      .set('@$', resolve('src'))

    const svgRule = config.module.rule('svg')
    svgRule.uses.clear()
    svgRule
      .oneOf('inline')
      .resourceQuery(/inline/)
      .use('vue-svg-icon-loader')
      .loader('vue-svg-icon-loader')
      .end()
      .end()
      .oneOf('external')
      .use('file-loader')
      .loader('file-loader')
      .options({
        name: 'assets/[name].[hash:8].[ext]'
      })

    const mdRule = config.module.rule('md')
    mdRule.test(/\.md$/)
      .use('html-loader')
      .loader('html-loader')
      .end()
      .use('markdown-loader')
      .loader('markdown-loader')
      .end()

    // if prod is on
    // assets require on cdn
    if (isProd) {
      config.plugin('html').tap(args => {
        args[0].cdn = assetsCDN
        return args
      })
    }
  },

  css: {
    loaderOptions: {
      less: {
        modifyVars: {
          // less vars，customize ant design theme

          'primary-color': '#000000',
          'link-color': '#000000',
          'border-radius-base': '2px'
        },
        // DO NOT REMOVE THIS LINE
        javascriptEnabled: true
      }
    }
  },
  // disable source map in production
  productionSourceMap: false,
  lintOnSave: undefined,
  // babel-loader no-ignore node_modules/*
  transpileDependencies: []
}

// preview.pro.loacg.com only do not use in your production;
if (process.env.VUE_APP_PREVIEW === 'true') {
  console.log('演示模式:', true)
  // add `ThemeColorReplacer` plugin to webpack plugins
  vueConfig.configureWebpack.plugins.push(createThemeColorReplacerPlugin())
}

//开发环境
if (NODE_ENV === 'development') {
  const VUE_APP_API_BASE_URL = process.env.VUE_APP_API_BASE_URL  // /api
  const VUE_APP_PROXY_URL = process.env.VUE_APP_PROXY_URL       //http://www.kiwipeach.cn/
  const VUE_APP_REWRITE_PATH = process.env.VUE_APP_REWRITE_PATH       // ^/api
  vueConfig.devServer = {
    // development server port 8000
    port: 9002,
    // If you want to turn on the proxy, please remove the mockjs /src/main.jsL11
    // http://localhost:8000/api/currentUser
    // http://www.kiwipeach.cn//currentUser
    proxy: {
      [VUE_APP_API_BASE_URL]: {
        target: VUE_APP_PROXY_URL, // target host
        ws: false,
        changeOrigin: true,
        pathRewrite: { [VUE_APP_REWRITE_PATH]: '' },    //从写地址，不带上/api上下文
        headers: {
          host: VUE_APP_PROXY_URL,
          origin: VUE_APP_PROXY_URL
        }
      }
    }
  }
}
console.log('开发环境配置:', JSON.stringify(vueConfig.devServer))
module.exports = vueConfig
